package com.ruoyi.web.controller.common;

import com.cdtu.room.domain.Calendar;
import com.cdtu.room.domain.Room;
import com.cdtu.room.dto.CalendarDto;
import com.cdtu.room.mapper.CalendarMapper;
import com.cdtu.room.mapper.RoomMapper;
import com.cdtu.room.service.ICalendarService;
import com.cdtu.room.vo.CalendarVo;
import com.ruoyi.system.domain.SysNotice;
import com.ruoyi.system.mapper.SysNoticeMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.List;

@Component
@Slf4j
public class RoomTaskController {
    @Autowired
    private CalendarMapper calendarMapper;
    @Autowired
    private ICalendarService calendarService;
    @Autowired
    private RoomMapper roomMapper;

    @Autowired
    private SysNoticeMapper sysNoticeMapper;

    // 定时任务，每天0点执行一次
    @Scheduled(cron = "0 0/30 * * * ?")
    public void updateMeetingRoomStatus() {
        // 获取当前日期
        Date now = new Date();
        // 查询日历表中的预定信息
        List<CalendarDto> scheduledEvents = calendarService.getCalendarList(new CalendarVo());

        // 遍历预定信息
        for (CalendarDto event : scheduledEvents) {
            // 如果当前日期处于预定日期内，则将会议室状态改为1
            if (now.after(event.getStartTime()) && now.before(event.getEndTime())) {
                Room room = new Room();
                BeanUtils.copyProperties(event,room);
                room.setStatus(1);
                roomMapper.updateRoom(room);
            }
            Room room = new Room();
            BeanUtils.copyProperties(event,room);
            room.setStatus(0);
            roomMapper.updateRoom(room);
        }
    }

    @Scheduled(cron = "0 0/1 * * * ? ")
    public String checkCalendarEntries() {
        // 获取当前时间
        Date now = new Date();
        // 查询即将开始的日历条目
        List<CalendarDto> upcomingEvents = calendarMapper.getUpcomingEvents(now);
        for (CalendarDto event : upcomingEvents) {
            // 如果开始时间距离当前时间不足一定阈值，发送提醒
            if (event.getStartTime().getTime() - now.getTime() <= 600000) { // 假设提前10分钟发送提醒
                String message = "您有一个会议即将开始：会议室名称为" + event.getRoomName();
                SysNotice sysNotice = new SysNotice();
                sysNotice.setStatus("0");
                sysNotice.setNoticeType("1");
                sysNotice.setNoticeTitle(message);
                sysNoticeMapper.insertNotice(sysNotice);
                return "你有一个新公告，请前往首页查看";
            }
        }

        // 查询已结束的日历条目并更新状态为2
        List<CalendarDto> endedEvents = calendarService.getEndedEvents(now);
        for (CalendarDto event : endedEvents) {
            if (event.getStatus() != 2){
                Calendar calendar = new Calendar();
                BeanUtils.copyProperties(event,calendar);
                calendar.setStatus(2);
                calendarService.updateCalendar(calendar);
            }
        }
        return null;
    }
}
